﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Effects;

namespace GlyphMaster
{
    public partial class MainPage : UserControl
    {
        GlyphProvider glyphs;
        Glyph currentGlyph;
        Boolean drawerOpened = true;

        public MainPage()
        {
            InitializeComponent();
            FillComboBox();
            InitializeGlyphs("xml/hiragana.xml");
        }

        private void InitializeGlyphs(String path)
        {
            glyphs = new GlyphProvider(path);
            FillGroups(glyphs.groups);
            LoadGlyphs();
        }

        private void FillComboBox()
        {
            ComboBoxItem cboItem = new ComboBoxItem();
            cboItem.Content = "Japanese (hiragana)";
            cboItem.Tag = "xml/hiragana.xml";
            cboLanguages.Items.Add(cboItem);
            cboItem = new ComboBoxItem();
            cboItem.Content = "Japanese (katakana)";
            cboItem.Tag = "xml/katakana.xml";
            cboLanguages.Items.Add(cboItem);
            cboItem = new ComboBoxItem();
            cboItem.Content = "Russian (cyrillic)";
            cboItem.Tag = "xml/russian.xml";
            cboLanguages.Items.Add(cboItem);
            cboLanguages.SelectedIndex = 0;
        }

        private void FillGroups(String[] groups)
        {
            leftBar.Children.Clear();
            Boolean first = true;
            foreach (String group in groups)
            {
                CheckBox checkBox = new CheckBox();
                checkBox.SetValue(CheckBox.ContentProperty, group);
                checkBox.SetValue(CheckBox.FontSizeProperty,12d);
                checkBox.SetValue(CheckBox.MarginProperty, new Thickness(5d, 0d, 0d, 10d));
                if (first)
                {
                    checkBox.SetValue(CheckBox.IsCheckedProperty, true);
                    first = false;
                }
                checkBox.Checked += new RoutedEventHandler(checkBox_CheckedChanged);
                checkBox.Unchecked += new RoutedEventHandler(checkBox_CheckedChanged);
                leftBar.Children.Add(checkBox);
            }
        }

        private void LoadGlyphs()
        {
            List<String> groups = new List<String>();
            foreach (CheckBox checkBox in leftBar.Children)
            {
                if ((Boolean)checkBox.GetValue(CheckBox.IsCheckedProperty))
                {
                    groups.Add((String)checkBox.GetValue(CheckBox.ContentProperty));
                }
            }
            glyphs.LoadGlyphs(groups.ToArray<String>());
            currentGlyph = glyphs.GetNext();
            lblCurrent.Text = currentGlyph.originalChar;
            LoadCheatSheet();
        }

        private void LoadCheatSheet()
        {
            drawerPanel.Children.Clear();
            foreach (Glyph glyph in glyphs.collection)
            {
                GlyphItem item = new GlyphItem(glyph);
                drawerPanel.Children.Add(item);
            }
        }

        void checkBox_CheckedChanged(object sender, RoutedEventArgs e)
        {
            LoadGlyphs();
        }

        private void txtInput_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.Enter:
                    if (txtInput.Text.ToUpper() == currentGlyph.transLiteration.ToUpper())
                    {
                        currentGlyph = glyphs.GetNext();
                        lblCurrent.Text = currentGlyph.originalChar;
                        txtInput.Text = "";
                        txtInput.Background = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255));
                    }
                    else
                    {
                        txtInput.Text = "";
                        txtInput.Background = new SolidColorBrush(Color.FromArgb(255, 200, 0, 0));
                    }
                    break;
            }
        }

        private void ToggleDrawer()
        {
            if (drawerOpened)
            {
                drawerClose.Begin();
                drawerOpened = false;
                txtCheatSheet.Text = "Show cheatsheet (or press spacebar)";
                scrollViewer.HorizontalScrollBarVisibility = ScrollBarVisibility.Hidden;
            }
            else
            {
                drawerOpen.Begin();
                drawerOpened = true;
                txtCheatSheet.Text = "Hide cheatsheet (or press spacebar)";
                scrollViewer.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
            }
        }

        private void Main_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.Space:
                    ToggleDrawer();
                    e.Handled = true;
                    break;
            }
        }

        private void Rectangle_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            ToggleDrawer();
            rctCheatSheet.Fill = new SolidColorBrush(Color.FromArgb(255, 76, 76, 76));
            txtCheatSheet.Foreground = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0));
        }

        private void Rectangle_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            rctCheatSheet.Fill = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0));
            txtCheatSheet.Foreground = new SolidColorBrush(Color.FromArgb(255, 76,76, 76));
        }

        private void cboLanguages_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            InitializeGlyphs(((ComboBoxItem)cboLanguages.SelectedItem).Tag.ToString());
        }

        private void Image_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            DropShadowEffect effect = new DropShadowEffect();
            effect.BlurRadius = 8;
            effect.Color = Color.FromArgb(255, 255, 255, 255);
            effect.Direction = 0;
            effect.ShadowDepth = 0;
            ((Image)sender).Effect = effect;
        }

        private void Image_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            ((Image)sender).Effect = null;
        }

        private void Image_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            switch (Application.Current.InstallState)
            {
                case InstallState.NotInstalled:
                    Application.Current.Install();
                    break;
                case InstallState.Installed:
                    MessageBox.Show("GlyphMaster is already installed on your computer");
                    break;
            }
        }
    }
}
